package com.app.micaihu.bean.news;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoHeightControllerListener;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.blankj.utilcode.util.StringUtils;

import static com.app.micaihu.R.id.item_three_layout;


/**
 * 大图话题
 * Created by linkai on 2017/3/17.
 */
public class ViewNewsTopicBig implements View.OnClickListener {


    public CustomImageView userhead;//作者头像
    public TextView username;//用户名字
    public TextView rankName;//军衔名称
    public CustomImageView rankIcon;//军衔图片
    public BiaoQinTextView title;//标题
    public CustomImageView bigImg;//一张大图
    public CustomImageView oneImg;//三图第一张
    public CustomImageView twoImg;//三图第二张
    public CustomImageView threeImg;//三图第三张
    public CustomImageView twoOneImg;//两图第一张
    public CustomImageView twoTwoImg;//两图第二张
    public View imgTopic;//话题图片
    public View threeLayout;
    public View twoimgLayout;
    public View allImgLayout;
    private TextView commentView;
    private TextView zanView;
    private ImageView ivHonourPic;
    private TextView tvArmyGroupInfo;

    private AutoHeightControllerListener autoHeightControllerListener;


    public ViewNewsTopicBig(View convertView) {
        initView(convertView);
    }


    /**
     * 初始化控件
     *
     * @param convertView
     */
    public void initView(View convertView) {

        this.title = (BiaoQinTextView) convertView.findViewById(R.id.item_public_title);
        this.userhead = (CustomImageView) convertView.findViewById(R.id.item_public_userhead);
        this.username = (TextView) convertView.findViewById(R.id.tv_nickname);
        this.rankIcon = (CustomImageView) convertView.findViewById(R.id.civ_rank);
        this.rankName = (TextView) convertView.findViewById(R.id.tv_rankname);
        this.bigImg = (CustomImageView) convertView.findViewById(R.id.item_big_image);
        this.oneImg = (CustomImageView) convertView.findViewById(R.id.item_image_0);
        this.twoImg = (CustomImageView) convertView.findViewById(R.id.item_image_1);
        this.threeImg = (CustomImageView) convertView.findViewById(R.id.item_image_2);
        this.threeLayout = convertView.findViewById(item_three_layout);
        this.twoimgLayout = convertView.findViewById(R.id.item_two_layout);
        this.twoOneImg = (CustomImageView) convertView.findViewById(R.id.item_two_image1);
        this.twoTwoImg = (CustomImageView) convertView.findViewById(R.id.item_two_image2);
        this.commentView = (TextView) convertView.findViewById(R.id.tv_commentnum);
        this.ivHonourPic = convertView.findViewById(R.id.ivHonourPic);
        this.tvArmyGroupInfo = convertView.findViewById(R.id.tvArmyGroupInfo);
        imgTopic = convertView.findViewById(R.id.img_topic);
        allImgLayout = convertView.findViewById(R.id.img_layout);
        this.zanView = (TextView) convertView.findViewById(R.id.tv_good);
        View view = convertView.findViewById(R.id.item_three_layout);
        ViewSizeUtils.changeH(view, 330);
        ViewSizeUtils.changeH(twoimgLayout, 330);
        autoHeightControllerListener = new AutoHeightControllerListener(this.bigImg, 4.35f, 1.58f);
        this.userhead.setOnClickListener(this);
        this.username.setOnClickListener(this);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity) {
        if (StringUtils.isEmpty(newsEntity.getHonourPic())) {
            this.ivHonourPic.setVisibility(View.GONE);
        } else {
            this.ivHonourPic.setVisibility(View.VISIBLE);
            ImageLoadUtils.loadImage(newsEntity.getHonourPic(), this.ivHonourPic);
        }
        this.tvArmyGroupInfo.setText(newsEntity.getArmyGroupInfo());
        LoadImageUtils.getInstance().loadWifiImage(this.userhead, newsEntity.getAuthorHeadPic());
        LoadImageUtils.getInstance().loadWifiImage(this.rankIcon, newsEntity.getAuthorRankIcon());
        if (!TextUtils.isEmpty(newsEntity.getArticleTitle())) {
            if (this.title.getVisibility() == View.GONE) {
                this.title.setVisibility(View.VISIBLE);
            }
            this.title.setPicText(newsEntity.getArticleTitle());
        } else {
            this.title.setVisibility(View.GONE);
        }
        this.rankName.setText(newsEntity.getAuthorRankName());
        this.username.setText(newsEntity.getAuthorName());
        this.username.setTag(newsEntity.getAuthorId());
        this.userhead.setTag(newsEntity.getAuthorId());
        if (newsEntity.getArticleThumb() == null || newsEntity.getArticleThumb().length == 0) {
            if (allImgLayout.getVisibility() == View.VISIBLE) {
                allImgLayout.setVisibility(View.GONE);
                imgTopic.setVisibility(View.GONE);
            }
        } else if (newsEntity.getArticleThumb().length == 1) {
            if (allImgLayout.getVisibility() == View.GONE) {
                allImgLayout.setVisibility(View.VISIBLE);
                imgTopic.setVisibility(View.VISIBLE);
            }
            if (this.bigImg.getVisibility() == View.GONE) {
                this.bigImg.setVisibility(View.VISIBLE);
            }
            LoadImageUtils.getInstance().loadWifiImage(this.bigImg, newsEntity.getArticleThumb()[0], autoHeightControllerListener);
            if (this.threeLayout.getVisibility() == View.VISIBLE) {
                this.threeLayout.setVisibility(View.GONE);
            }
            if (this.twoimgLayout.getVisibility() == View.VISIBLE) {
                this.twoimgLayout.setVisibility(View.GONE);
            }
        } else if (newsEntity.getArticleThumb().length == 2) {
            if (allImgLayout.getVisibility() == View.GONE) {
                allImgLayout.setVisibility(View.VISIBLE);
                imgTopic.setVisibility(View.VISIBLE);
            }
            if (this.bigImg.getVisibility() == View.VISIBLE) {
                this.bigImg.setVisibility(View.GONE);
            }
            if (this.threeLayout.getVisibility() == View.VISIBLE) {
                this.threeLayout.setVisibility(View.GONE);
            }
            if (this.twoimgLayout.getVisibility() == View.GONE) {
                this.twoimgLayout.setVisibility(View.VISIBLE);
            }
            LoadImageUtils.getInstance().loadWifiImage(this.twoOneImg, newsEntity.getArticleThumb()[0]);
            LoadImageUtils.getInstance().loadWifiImage(this.twoTwoImg, newsEntity.getArticleThumb()[1]);
        } else if (newsEntity.getArticleThumb().length >= 3) {
            if (allImgLayout.getVisibility() == View.GONE) {
                allImgLayout.setVisibility(View.VISIBLE);
                imgTopic.setVisibility(View.VISIBLE);
            }
            if (this.bigImg.getVisibility() == View.VISIBLE) {
                this.bigImg.setVisibility(View.GONE);
            }
            if (this.threeLayout.getVisibility() == View.GONE) {
                this.threeLayout.setVisibility(View.VISIBLE);
            }
            if (this.twoimgLayout.getVisibility() == View.VISIBLE) {
                this.twoimgLayout.setVisibility(View.GONE);
            }
            LoadImageUtils.getInstance().loadWifiImage(this.oneImg, newsEntity.getArticleThumb()[0]);
            LoadImageUtils.getInstance().loadWifiImage(this.twoImg, newsEntity.getArticleThumb()[1]);
            LoadImageUtils.getInstance().loadWifiImage(this.threeImg, newsEntity.getArticleThumb()[2]);
        }

        this.commentView.setText(newsEntity.getCommentNum() + "评论");
        this.zanView.setText(newsEntity.getPraiseNum());
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.item_public_userhead:
            case R.id.tv_nickname:
                //进入个人主页
                try {
                    String uid = v.getTag().toString();
                    MyHomepageDynamicActivity.goToUserHomePage((Activity) v.getContext(), uid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
